package danny.hoi.web.controller.system;


import com.github.pagehelper.PageInfo;
import danny.hoi.domain.export.system.Dept;
import danny.hoi.domain.export.system.DeptExample;
import danny.hoi.service.export.system.DeptService;
import danny.hoi.web.controller.base.BaseController;
import danny.hoi.web.utils.IsParentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    /**
     * 1.分页显示信息
     * 2.请求地址:dept/list.do
     * 3.响应地址:WEB-INF/pages/system/dept/dept-list.jsp
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       String key) {
        if (key == null) {
            key = "";
        }
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();
        //设置条件查询
        criteria.andDeptNameLike("%" + key + "%");
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        PageInfo<Dept> pageInfo = deptService.findByPages(example, pageNum, pageSize);

        request.setAttribute("key", key);
        request.setAttribute("pageInfo", pageInfo);
        return "system/dept/dept-list";
    }

    /**
     * 1.前往添加页面
     * 2.请求地址:/dept/toAdd.do
     * 3.响应地址:WEB-INF/pages/dept/dept-add.jsp
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        List<Dept> deptList = deptService.findAll(example);
        request.setAttribute("deptList", deptList);
        return "system/dept/dept-add";
    }

    /**
     * 1.前往修改页面
     * 2.请求地址:/dept/toUpdate.do
     * 3.响应地址:WEB-INF/pages/dept/dept-update.jsp
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        List<Dept> deptList = deptService.findAll(example);
        //去掉当前部门和该部门的子部们
        Dept dept = deptService.findById(id);
        Iterator<Dept> iterator = deptList.iterator();
        while (iterator.hasNext()) {
            Dept temp = iterator.next();
            if (IsParentUtils.isParent(dept, temp)) {
                iterator.remove();
            }
        }
        request.setAttribute("dept", dept);
        request.setAttribute("deptList", deptList);
        return "system/dept/dept-update";
    }


    /**
     * 1.更新页面
     * 2.请求地址:/dept/edit.do
     * 3.响应地址:redirect:/dept/list.do
     */
    @RequestMapping("/edit")
    public String edit(Dept dept) {
        dept.setCompanyId(getLoginCompanyId());
        dept.setCompanyName(getLoginCompanyName());
        if ("".equals(dept.getParentId())) {
            dept.setParentId(null);
        }
        if (StringUtils.isEmpty(dept.getDeptId())) {
            //添加
            deptService.save(dept);
        } else {
            //更新
            deptService.update(dept);
        }

        return "redirect:/system/dept/list.do";
    }

    /**
     * 1.删除
     * 2.请求地址:"/system/dept/delete.do?id="+id,
     * 3.响应地址:异步响应
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(String id) {
        HashMap<String, String> map = new HashMap<>();
        //判断当前部门是不是其他部门的父部门
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<Dept> depts = deptService.findAll(example);
        if (depts == null || depts.size() == 0) {
            //没有子部门,可以删除
            deptService.delete(id);
            map.put("msg", "恭喜您删除成功");
        } else {
            map.put("msg", "删除失败,请删除该部门的子部门");
        }
        return map;
    }


}
